package edu.vt.ece_3574.amber_alert.location;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.widget.Toast;
import edu.vt.ece_3574.amber_alert.AmberAlertApplication;
import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.curfew.CurfewMessageDisplayer;
import edu.vt.ece_3574.amber_alert.database.DatabaseHandler;
import edu.vt.ece_3574.amber_alert.database.LocateAccount;

public class LocationServerConnector implements Runnable {
	private Account account;
	
	private String useForLocation;

	private Activity startingContextActivity;
	
	public LocationServerConnector(Account account, String useForLocation, Activity startingContextActivity) {
		this.account = account;
		this.useForLocation = useForLocation;
		this.startingContextActivity = startingContextActivity;
	}
	
	public void run() {
		notifyServerToLocateAccount();
		waitForServerToBeUpdated();
		handleLocation();
		
	}

	private void handleLocation() {
		if (useForLocation.equals("DisplayLocation")) {
			LocationDisplayerActivity.mapPoint = DatabaseHandler.get().getLocationFromServer(account);
			Intent intent = new Intent(startingContextActivity, LocationDisplayerActivity.class);
			startingContextActivity.startActivity(intent);
			startingContextActivity.finish();
		}
		else if (useForLocation.equals("Curfew")) {
			String message = CurfewMessageDisplayer.get().getToastMessage(account);
			//Determine whether the member has met their curfew and show the toast specifying accordingly
			showToast(message);
//			Intent intent = new Intent(AmberAlertApplication.getContext(), PrimaryMain.class);
//			PendingIntent pIntent = PendingIntent.getActivity(AmberAlertApplication.getContext(), 0, intent, 0);
//			Notification notification = new Notification(R.drawable.ic_media_play, message, System.currentTimeMillis());
//			notification.setLatestEventInfo(AmberAlertApplication.getContext(), "Curfew Message", message, pIntent);
//			notification.defaults = Notification.DEFAULT_ALL;
//			manager.notify(0, notification);
		}
	}
	
	private void showToast(String message) {
		//Currently we are in a service thread, so toasts cannot be displayed. This code handles displaying the toast in the UI thread
		Thread uiThread = new HandlerThread("UIHandler"); 
	    uiThread.start(); 
	    UIHandler uiHandler = new UIHandler(((HandlerThread) uiThread).getLooper()); 
		
	    Message msg = uiHandler.obtainMessage(UIHandler.DISPLAY_UI_TOAST); 
	    msg.obj = message; 
	    uiHandler.sendMessage(msg); 
	}
	
	private final class UIHandler extends Handler 
	{ 
	    public static final int DISPLAY_UI_TOAST = 0; 
	 
	    public UIHandler(Looper looper) 
	    { 
	        super(looper); 
	    } 
	 
	    @Override 
	    public void handleMessage(Message msg) 
	    { 
	        switch(msg.what) 
	        { 
	        case UIHandler.DISPLAY_UI_TOAST: 
	        { 
	            Context context = AmberAlertApplication.getContext();

	            //Display the toast with the appropriate message
	            Toast t = Toast.makeText(context, (String)msg.obj, Toast.LENGTH_LONG); 
	            t.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
	            t.show(); 
	        } 
	        default: 
	            break; 
	        } 
	    } 
	} 

	private void waitForServerToBeUpdated() {
		PendingLocateAccount pendingLocateAccount = PendingLocateAccount.get();
		pendingLocateAccount.reset();
		pendingLocateAccount.await();
	}

	private void notifyServerToLocateAccount() {
		try {
			new LocateAccount().execute(account);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
